﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService.ViewDatas;
using DBService;

public partial class Faq : System.Web.UI.Page
{
    private string rownumber;
    private int count = 0;
    private int questionID;
    private int categoryID;
    private List<Question> question_List;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int.TryParse((string)Request.QueryString["id"], out questionID);
            if (int.TryParse((string)Request.QueryString["categoryid"], out categoryID))
            {
                BindCategoryRepeater();
                BindQuestionRepeaterByCategoryID();
            }
            else
            {
                BindCategoryRepeater();
                BindQuestionRepeater();
            }
        }
    }

    protected void SeachButton_Click(object sender, EventArgs e)
    {

    }

    public void QuestionCategoryRep_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (QuestionCategory)e.Item.DataItem;
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("QusetionCategoryItem");
            lit.Text = "<div class=\"ques_label\"><a href=\"Faq.aspx?categoryid=" + item.QuestionCategoryID + "\">" + item.Title + "</a></div>";
        }

    }

    public void QuestionRep_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        count++;
        string answerVis = "";
        string littext = "";
        if (count < 10)
        {
            rownumber = "0" + count.ToString();
        }
        else
        {
            rownumber = count.ToString();
        }
        var item = (Question)e.Item.DataItem;
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("QusetionItem");
            littext = "<div class=\"ques_numb\"><span class=\"list_n\">" + rownumber + "</span> <span class=\"list_quest_act\"><a class=\"ClickONQuet\" href=\"Faq.aspx?id=" + item.QestionID + "\">" + item.QuestionText
                + "</a>";
            answerVis = "</span><div style=\"display:none;\" class=\"quest_ans\">" + item.Answer + "</div>";
            lit.Text = littext + answerVis + "</div>";

        }
    }
    private void BindCategoryRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetQuestionCategroy(null);

        QuestionCategoryRep.DataSource = coll;

        QuestionCategoryRep.DataBind();

    }

    protected void OnSubmit(object sender, EventArgs e)
    {
        ModelAdapter service = new ModelAdapter();
        service.QuestionAction(null, null, Name_box.Value, mail_box.Value, textarea.Value, false);
    }

    protected void OnSearch(object sender, EventArgs e)
    {
        ModelAdapter service = new ModelAdapter();
        if (SearchTextID.Value == "Search your Question")
        {
            return;
        }
        else
        {
            question_List = service.GetQuestionOnSerch(SearchTextID.Value);
            BindQuestionRepeater();
        }
    }


    public void BindQuestionRepeater()
    {
        var service = new ModelAdapter();
        if (question_List == null)
        {
            question_List = service.GetQuestion(null);
        }
        QuestionRep.DataSource = question_List;

        QuestionRep.DataBind();


    }
    public void BindQuestionRepeaterByCategoryID()
    {
        var service = new ModelAdapter();
        var coll = service.GetQuestionByCategoryID(categoryID);

        QuestionRep.DataSource = coll;

        QuestionRep.DataBind();

    }

}
